package com.example.ch03_2;

import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class flowerservice {
    public static List<Flower> getFromXML(InputStream is)throws Exception {

        //得到解析器
        XmlPullParser parser= Xml.newPullParser();
        //初始化解析器，第一个参数代表包含xml的数据

        parser.setInput(is,"utf-8");
        List<Flower> flowers=null;
        Flower flower=null;
            int event=parser.getEventType();

            while (event!=XmlPullParser.END_DOCUMENT){
                switch (event){
                    case XmlPullParser.START_TAG:
                        if("infos".equals(parser.getName())){
                            flowers=new ArrayList<Flower>();
                        }else if("flower".equals(parser.getName())){
                            flower=new Flower();
                            String id=parser.getAttributeValue(0);
                            flower.setId(id);
                        }else if("sum".equals(parser.getName())){
                            String sum=parser.nextText();
                            flower.setSum(sum);
                        }
                        break;
                    case  XmlPullParser.END_TAG:
                        if("flower".equals(parser.getName())){
                            flowers.add(flower);
                            flower=null;
                        }


                        break;
                }
                event=parser.next();
            }

        return flowers;
    }

}
